#!/usr/bin/env python

from user_class import User

def get_templates ( liwe ):
	"""
        :INPUT:
	"""
	uc = User ( liwe )

	return { "templates" : uc.get_templates () }

def login ( liwe, login = '', pwd = '', email = '', check_session = True ):
	"""
	:INPUT:

		- login:		SAFE_DB_STRING
		- pwd:			SAFE_DB_STRING
		- email:		SAFE_DB_STRING
		- check_session:	SAFE_BOOL
	"""

	#FIXME Vogliamo loggare sia con login che con pwd?
	if not email and '@' in login:
		email = login
		login = None

	uc = User ( liwe )
	return uc.login ( login, pwd, email, check_session )

def logout ( liwe ):
	uc = User ( liwe )
	return uc.logout ()

def change_password ( liwe, pwd = '', email = '', token = '' ):
	"""
	:INPUT:
		- pwd:			SAFE_DB_STRING
		- email:		SAFE_DB_STRING
		- token:		SAFE_STRING
	"""

	uc = User ( liwe )
	return uc.change_password ( new_pwd = pwd, email = email, token = token )


def modify ( liwe, login = '', oldpwd = '', email = '', pwd = ''):
	"""
	:INPUT:
		- login:	SAFE_DB_STRING
		- email:	SAFE_DB_STRING
		- pwd:		SAFE_DB_STRING
		- oldpwd:	SAFE_DB_STRING
	"""
	uc = User ( liwe )

	res = uc.modify ( login = login, oldpwd = oldpwd, email = email, pwd = pwd )

	return res 

def login_exists ( liwe, login = '', email = '' ):
	"""
	:INPUT:
		- login:		SAFE_DB_STRING
		- email:		SAFE_DB_STRING
	"""
	uc = User ( liwe )
	return { "exists" : uc.login_exists ( login = login, email = email ) }

def get_info ( liwe ):
	"""
        :INPUT:
	"""
	uc = User ( liwe )
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	return uc.get_info ( uid )

def get_full_info ( liwe ):
	uc = User ( liwe )
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	data = uc.get_info ( uid ) 
	data [ 'user_data' ] = uc.user_data_get ( uid = uid ) 
	return data

def update ( liwe, uid, login = '', pwd = '', email = '', enabled = 1 ):
	"""
	:INPUT:
		- uid:			SAFE_NUMBER
		- login:		SAFE_DB_STRING
		- pwd:			SAFE_DB_STRING
		- email:		SAFE_DB_STRING
		- enabled:		SAFE_DB_BOOL
	"""

	uc = User ( liwe )
	return uc.update ( uid, login, pwd, email, enabled )

def lost_password ( liwe, email = '' ):
	"""
	:INPUT:
		- email:		SAFE_DB_STRING
	"""
	uc = User ( liwe )
	return uc.lost_password ( email )

def is_logged ( liwe ):
	"""
	:INPUT: 
	"""
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	mail = liwe.pyhp.session.data.get ( "email", '' )
	if uid == -1:
		return {}
	else:
		_gr =  liwe.pyhp.session.data.get ( "groups" , [] );
		return { "uid" : uid , 'email' : mail,  "groups" : _gr }

def register ( liwe, email, pwd, pwd2, login = '' ):
	"""
	:INPUT:
		- email:		SAFE_DB_STRING
		- pwd:			SAFE_DB_STRING
		- pwd2:			SAFE_DB_STRING
		- login:		SAFE_DB_STRING
	"""

	if not login: login = email

	if pwd != pwd2:
		liwe.err_descr = "Password errata"
		liwe.err_code = 1
		return {}

	if not login:
		login = email

	u = User ( liwe )
	uid = u.create ( login = login, passwd = pwd, email = email )

	return { "uid" : uid }

def facebook_login ( liwe, access_token ):
	"""
	:INPUT:
		- access_token:	SAFE_STRING
	"""
	u = User ( liwe )
	res = u.facebook_login ( access_token )

	return { "text" : res }

def session_update ( liwe ):
	"""
	:INPUT:
	"""
	u = User ( liwe )
	u.session_update ()

	return {}

def user_data_set ( liwe, id_data_type = 0, value = '', id_user_data = 0 ):
	"""
	:INPUT:
		- id_data_type:	SAFE_NUMBER
		- value:	SAFE_STRING
		- id_user_data:	SAFE_NUMBER
	"""
	u = User ( liwe )
	uid = liwe.get_uid ();
	res = u.user_data_set ( uid = uid, id_data_type = id_data_type, value = value, id_user_data = id_user_data ) 	
	return { 'user_data' : res } 

def user_data_get ( liwe, id_data_type = 0 ):
	"""
	:INPUT:
		- id_data_type:	SAFE_NUMBER
	"""
	u = User ( liwe )
	uid = liwe.get_uid ();
	return { 'user_data' : u.user_data_get ( uid = uid, id_data_type = id_data_type ) }	

def user_data_del ( liwe, id_data_user = 0 ):
	"""
	:INPUT:
		- id_data_type:	SAFE_NUMBER
	"""
	u = User ( liwe )
	uid = liwe.get_uid ();
	u.user_data_del ( uid = uid, id_data_user= id_data_user ) 	

def user_data_type_list ( liwe ):
	u = User ( liwe )
	return { 'data_type' : u.user_data_type_list () }

def user_data_type_get ( liwe, id_data_type = 0, name = 0 ):
	"""
	:INPUT:
		- id_data_type:	SAFE_NUMBER
		- name:		SAFE_DB_STRING
	"""
	u = User ( liwe )
	return { 'data_type' : u.user_data_type_get ( id_data_type = id_data_type, name = name ) }
	



